
import httpUtils from "./XmlHttpTool";
cc.Class({

    extends: cc.Component,
    properties: {

        shopImageUrl:cc.Sprite,
        shopName:cc.Label,
        shopStatus:cc.Integer,
        chargeBtn:cc.Button,
        shopStar:cc.Label,
        goodDicjson:null,
        staricon:cc.Sprite,
        alreadyCharge:cc.Node
    },

    onLoad () {

        var clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
        clickEventHandler.component = "goodItems";//这个是代码文件名
        clickEventHandler.handler = "chargegoTo";
        this.chargeBtn.clickEvents.push(clickEventHandler);
    },


    chargegoTo:function(){


        var changebig2 = cc.scaleTo(0.2,1.1);

        var finish = cc.callFunc(function(){
    
          var changesmail2 = cc.scaleTo(0.1,1.0);
          this.chargeBtn.node.runAction(changesmail2)
    
       }, this);
    
       var seq = cc.sequence(changebig2,finish)
    
       this.chargeBtn.node.runAction(seq)

        
        var that =this

        httpUtils.NhttpPost("/marmot/prize_exchange",{"tid":that.goodDicjson["id"]},function(res){

            cc.find("Canvas/shopAllView").active=false
            var cranNode=cc.find("Canvas/addressAllView");
            console.log("兑换成功",res)
            cc.find("Canvas/addressAllView/AddressView/LocationView").getComponent("AddressView").init(that.goodDicjson["id"],res,true)
            cranNode.active=true
           
   
        },function(failres){

            var failecranNode=cc.find("Canvas/faileAlertView");
            failecranNode.active=true
            console.log("兑换失败",failres)
        })

    },
   
    init: function (goodDic) {


        this.goodDicjson=goodDic
        this.shopName.getComponent(cc.Label).string=goodDic["name"]
        this.shopStar.getComponent(cc.Label).string="x"+goodDic["quantity"]

        var that = this

        cc.loader.load({url: goodDic["thumbnail"], type: 'png'}, function(err,img){

            var mylogo  = new cc.SpriteFrame(img); 

            that.shopImageUrl.getComponent(cc.Sprite).spriteFrame = mylogo;
            
        });

        if(goodDic["quantity"]<0){

         cc.loader.loadRes("已兑完星币", cc.SpriteFrame, function (err, spriteFrame) {
            that.staricon.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        });
        this.alreadyCharge.active=true
        this.chargeBtn.node.active=false
       

        }else{

            this.alreadyCharge.active=false
            this.chargeBtn.node.active=true
        
        }
   
    }

});